package cn.itsource.pet.controller;

import cn.itsource.pet.dto.MsgDto;
import cn.itsource.pet.service.ISearchMasterMsgService;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.query.SearchMasterMsgQuery;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    /**
     * 接口：添加或修改
     * @param searchMasterMsg  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){ 
        if( searchMasterMsg.getId()!=null){
		searchMasterMsgService.update(searchMasterMsg);
        }else{
                searchMasterMsgService.add(searchMasterMsg);
	}
        return new AjaxResult();
       
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        searchMasterMsgService.del(id);
        return new AjaxResult();
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
        searchMasterMsgService.patchDel(ids);
        return new AjaxResult();
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public SearchMasterMsg findOne(@PathVariable("id")Long id) {
        return searchMasterMsgService.findOne(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<SearchMasterMsg> findAll(){
        return searchMasterMsgService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.queryPage(query);
    }

    /** 审核寻主消息 */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg msg, HttpServletRequest request) {
        System.out.println(111);
        return searchMasterMsgService.publish(msg,request);
    }

    /**
     * 接口：待审核
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        return searchMasterMsgService.user(query,request);
    }
    /**
     * 接口：待处理
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        return searchMasterMsgService.toHandle(query,request);
    }
    /**
     * 接口：寻主池
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/toSearchMasterPool")
    public PageList<SearchMasterMsg> toSearchMasterPool(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.toSearchMasterPool(query);
    }
    /**
     * 接口：已完成
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        return searchMasterMsgService.finish(query,request);
    }

    /** 拒单 */
    @GetMapping("/reject/{id}")
    public AjaxResult reject(@PathVariable("id") Long id) {
        return searchMasterMsgService.reject(id);
    }

    /** 接单 */
    @PostMapping("/accept")
    public AjaxResult accept(MsgDto dto) {
        return searchMasterMsgService.accept(dto);
    }
}
